package com.example.ephuizi.stl.util.photo_3d;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class SealImage {

    private static final int SIDE_WIDTH = 300;
    private static final int BORDER_WIDTH = 15;
    private static final int PAD_WIDTH_1 = 20;
    private static final int PAD_WIDTH_2 = 15;
    private static final int PAD_WIDTH_3 = 15;
    private static final int PAD_WIDTH_4 = 10;

    /**
     * 300*300dmm
     *
     * @param s
     */
    public static void generateBitmap(String s, int color, Typeface typeface, String savePath) {
        if (s == null || s.isEmpty()) {
            return;
        }
        Bitmap bitmap = Bitmap.createBitmap(SIDE_WIDTH, SIDE_WIDTH, Config.ARGB_8888);
        SealImage.addBorder(bitmap, color);

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setTypeface(typeface);
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);

        int center = SIDE_WIDTH / 2;
        switch (s.length()) {
            case 1:
                // | 1 |
                int min1 = BORDER_WIDTH + PAD_WIDTH_1;
                int max1 = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_1;
                paint.setTextSize(max1 - min1);
                //estimated:baseline=字体高度/6
                int baseline1 = (max1 - min1) / 6;
                canvas.drawText(s, min1, max1 - baseline1, paint);
                break;
            case 2:
                // | 1 2 |
                // 第一个字起点
                int b21x = BORDER_WIDTH + PAD_WIDTH_2;
                int b21y = b21x;
                int e21x = center - PAD_WIDTH_2 / 2;
                int e21y = e21x;
                // 第二个字起点
                int b22x = center + PAD_WIDTH_2 / 2;
                int e22x = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_2;
                int e22y = e21y;

                paint.setTextSize(e21x - b21x);
                int baseline2 = (e21x - b21x) / 6;

                // 字体右序
                String s21 = s.substring(1, 2);
                String s22 = s.substring(0, 1);

                canvas.drawText(s21, b21x, e21y - baseline2, paint);
                canvas.drawText(s22, b22x, e22y - baseline2, paint);

                int newEndY2 = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_2;
                tensileY(bitmap, b21x, b21y, e22x, e22y, newEndY2);
                break;

            case 3:
                // |*1乙***|
                // |***2甲*|
                // |*3丙***|
                // 字体右序--甲乙丙
                String s31 = s.substring(1, 2);// 乙
                String s32 = s.substring(0, 1);// 甲
                String s33 = s.substring(2, 3);// 丙

                // 第一个字
                int b31x = BORDER_WIDTH + PAD_WIDTH_3;
                int b31y = b31x;
                int e31x = center - PAD_WIDTH_3 / 2;
                int e31y = e31x;

                int textSize3 = e31x - b31x;
                int baseline3 = textSize3 / 6;
                paint.setTextSize(textSize3);

                // 第二个字
                int b32x = center + PAD_WIDTH_3 / 2;
                int b32y = b31y;
                int e32x = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_3;
                int e32y = e31y;

                canvas.drawText(s32, b32x, e32y - baseline3, paint);
                int newEndY3 = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_3;
                // 先完成拉伸
                tensileY(bitmap, b32x, b32y, e32x, e32y, newEndY3);

                // 第三个字
                int b33x = b31x;
                int e33y = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_3;

                canvas.drawText(s31, b31x, e31y - baseline3, paint);
                canvas.drawText(s33, b33x, e33y - baseline3, paint);
                break;

            default:
                // |*1丙2甲*|
                // |*3丁4乙*|
                // 字体右序--甲乙丙丁
                String s41 = s.substring(2, 3);// 丙
                String s42 = s.substring(0, 1);// 甲
                String s43 = s.substring(3, 4);// 丁
                String s44 = s.substring(1, 2);// 乙

                // 第一个字
                int b41x = BORDER_WIDTH + PAD_WIDTH_4;
                int b41y = b41x;
                int e41x = center - PAD_WIDTH_4 / 2;
                int e41y = e41x;

                int b42x = center + PAD_WIDTH_4 / 2;
                int b42y = b41y;
                int e42y = e41y;

                int b43x = b41x;
                int b43y = center + PAD_WIDTH_4 / 2;
                int e43y = SIDE_WIDTH - BORDER_WIDTH - PAD_WIDTH_4;

                int b44x = b42x;
                int b44y = b43y;
                int e44y = e43y;

                int textSize4 = e41x - b41x;
                paint.setTextSize(textSize4);
                int baseline4 = (textSize4) / 6;

                canvas.drawText(s41, b41x, e41y - baseline4, paint);
                canvas.drawText(s42, b42x, e42y - baseline4, paint);
                canvas.drawText(s43, b43x, e43y - baseline4, paint);
                canvas.drawText(s44, b44x, e44y - baseline4, paint);
                break;
        }
        File f = new File(savePath);
        try {
            if (!f.exists())
                f.createNewFile();
            OutputStream stream = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        } catch (IOException e) {
            Log.e(SealImage.class.getSimpleName(), "generateBitmap " + e.toString());
        }

    }

    private static void addBorder(Bitmap bitmap, int color) {
        int max = SIDE_WIDTH - BORDER_WIDTH;
        for (int x = 0; x < bitmap.getWidth(); x++) {
            for (int y = 0; y < bitmap.getWidth(); y++) {
                if (!(x >= BORDER_WIDTH && y >= BORDER_WIDTH && x < max && y < max))
                    bitmap.setPixel(x, y, color);
            }
        }
    }

    /**
     * 指定区域拉伸y轴
     *
     * @param bitmap
     * @param beginX
     * @param beginY
     * @param oldEndX
     * @param oldEndY
     * @param newEndY
     */
    private static void tensileY(Bitmap bitmap, int beginX, int beginY, int oldEndX, int oldEndY, int newEndY) {
        // 利用倒序
        for (int x = oldEndX; x >= beginX; x--) {
            for (int y = newEndY; y >= beginY; y--) {
                bitmap.setPixel(x, y, bitmap.getPixel(x, ((oldEndY - beginY) * (y - beginY) / (newEndY - beginY) + beginY)));
            }
        }
    }
}
